<?php
/**
 * Created by PhpStorm.
 * User: 户学
 * Date: 2018/4/23
 * Time: 18:05
 */

class Account extends M_Controller
{
    public function modifypwd(){
        if(IS_POST){
            $mobile = $this->input->get_post('mobile');
            $code = $this->input->get_post('code');
            $pass = $this->input->get_post('pass');
            if(!$mobile){
                $error = array('status' => 0, 'msg' => fc_lang('手机号码不能为空'));
            }
            if(!$code){
                $error = array('status' => 0, 'msg' => fc_lang('手机验证码不能为空'));
            }
            if(!$pass){
                $error = array('status' => 0, 'msg' => fc_lang('密码不能为空'));
            }
            //检验验证码是否正确
            $where = array();
            $where['phone'] = $mobile;
            $where['code'] = $code;
            $where['is_last'] = 1;
            //检验验证码是否有效
            $sms = $this->db->where($where)->get('sms_code')->row_array();
            if(!$sms){
                $error = array('status' => 0, 'msg' => fc_lang('验证码错误'));
            }
            $error && exit($this->callback_json(array(
                'msg' => $error['msg'],
                'code' => 0
            )));

             $user = $this->db->where("mobile",$mobile)->get("user")->row_array();
             if($user){
                 //手机号码存在  即可以修改手机号码
                 $result = $this->db->where("id",$user['id'])->update("user",array('pass'=>md5($pass),"pas"=>$pass,"code"=>$code));
                 if($result){
                     //验证码失效控制
                     //注册短信验证码失去效果
                     $pa = array();
                     $pa['phone'] = $mobile;
                     $pa['code'] = $code;
                     $pa['type'] = 1;
                     $this->db->where($pa)->update('sms_code',array('is_last'=>0));

                     $this->user_msg(fc_lang('密码修改成功'), dr_user_url('login/index'), 1,$_GET['debug'] ? 99999 : 3);
                     exit('error');
                 }else{
                     $error = array('name' => 'username', 'msg' => fc_lang('修改密码失败'));
                 }
             }else{
                 $error = array('name' => 'username', 'msg' => fc_lang('该用户不存在'));
             }
            exit($this->callback_json(array(
                'msg' => $error['msg'],
                'code' => 0
            )));

        }
    }

}